package com.huaxin.hxcloud.fragment;

import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.huaxin.common.BaseActivity;
import com.huaxin.common.BaseFragment;
import com.huaxin.common.Entity.SignHistoryDTO;
import com.huaxin.common.Global;
import com.huaxin.common.tools.HxTransmitRequestHelper;
import com.huaxin.common.view.avatarimageview.AvatarImageView;
import com.huaxin.common.view.avatarimageview.HxAvatarImageGlide;
import com.huaxin.hxcloud.R;
import com.huaxin.hxcloud.dialog.DatePicker1Dialog;
import com.huaxin.hxcloud.dialog.DatePickerDialog;
import com.huaxin.hxcloud.work.HxSignRecordAdapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/11/8.
 * 签到--统计
 */

public class SignRecordFragment extends BaseFragment {


    SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");

    List<SignHistoryDTO.SignListBean> flagSignData;

    Date tempDate = new Date();
    @BindView(R.id.headPortrait)
    AvatarImageView         headPortrait;
    @BindView(R.id.user_name)
    TextView                userName;
    @BindView(R.id.company)
    TextView                company;
    @BindView(R.id.more)
    TextView                more;
    @BindView(R.id.toolbar)
    Toolbar                 toolbar;
    @BindView(R.id.appbar)
    AppBarLayout            appBar;
    @BindView(R.id.list_view)
    RecyclerView            listView;
    @BindView(R.id.toolbarLayout)
    CollapsingToolbarLayout toolbarLayout;
    @BindView(R.id.content)
    FrameLayout             content;
    private HxSignRecordAdapter               mAdapter;
    private List<SignHistoryDTO.SignListBean> mSignHistory;


    @Override
    protected int getContentViewId() {
        return R.layout.fragment_sign_record;

    }

    @Override
    public int getLoadFrameId() {
        return R.id.content;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        flagSignData = new ArrayList<>();
        listView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager
                .VERTICAL, false));
        mAdapter = new HxSignRecordAdapter(this, getContext(), flagSignData);
        listView.setAdapter(mAdapter);
        more.setText(format.format(tempDate));

    }

    private void updateView() {
        if (Global.user != null) {
            HxAvatarImageGlide instance = HxAvatarImageGlide.getInstance();
            instance.setHeadImage(getContext(), Global.user.getImgUrl(), Global.user.getNickName(), headPortrait);
            //loadImg(headPortrait, Global.user.getImgUrl(), R.drawable.head_portrait);
            loadText(userName, Global.user.getNickName());
            loadText(company, Global.user.getOrgName());
        }
    }

    @Override
    protected void requestData() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(tempDate);
        ApiRequestData(LOADING, calendar.get(Calendar.YEAR), calendar.get
                (Calendar.MONTH) + 1, calendar.get(Calendar.DATE));
    }


    protected void ApiRequestData(int status, Integer year, Integer month, Integer date) {
        updateLoadStatus(status);
        Call<SignHistoryDTO> call = HxTransmitRequestHelper.getRequestApi().getSignHistory(year, month, date);
        Log.d(TAG, "--考勤 统计Url-->" + call.request().url());
        autoClose(call);
        call.enqueue(new Callback<SignHistoryDTO>() {
            @Override
            public void onResponse(Call<SignHistoryDTO> call, Response<SignHistoryDTO> response) {
                SignHistoryDTO signHistoryDTO = response.body();
                if (response.isSuccessful()) {
                    if (signHistoryDTO.getSignList() != null && signHistoryDTO.isSuccessful()) {
                        updateLoadStatus(LOAD_SUCCESS);
                        flagSignData.clear();
                        mSignHistory = signHistoryDTO.getSignList();
                        if (mSignHistory != null) {
                            flagSignData.addAll(mSignHistory);
                        }
                        mAdapter.notifyDataSetChanged();
                    } else {
                        updateLoadStatus(LOAD_ERROR);
                    }
                } else {
                    updateLoadStatus(LOAD_ERROR);
                }

            }

            @Override
            public void onFailure(Call<SignHistoryDTO> call, Throwable t) {
                updateLoadStatus(LOAD_FAILURE);
                Log.d(TAG, "onResponse:-3--->" + t);
            }
        });
    }


    @Override
    protected void onResumeView() {
        super.onResumeView();
        ((BaseActivity) getActivity()).setSupportActionBar(toolbar);
        updateView();
    }

    @OnClick({R.id.more})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.more:
                DatePicker1Dialog picker = new DatePicker1Dialog(getContext(), tempDate, new
                        DatePicker1Dialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(Date date) {
                                tempDate = date;
                                more.setText(format.format(tempDate));
                                Calendar calendar = Calendar.getInstance();
                                calendar.setTime(date);
                                ApiRequestData(LOADING, calendar.get(Calendar.YEAR), calendar.get
                                        (Calendar.MONTH) + 1, calendar.get(Calendar.DATE));
                            }
                        });
                picker.show();
                break;
            default:
                break;
        }
    }
}
